/**Copyright (c) 2010, Isidoro Trevino de la Garza (chololo@gmail.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:
(1)Redistributions of source code must retain the above copyright notice, this list 
of conditions and the following disclaimer. 
(2)Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or other 
materials provided with the distribution. 
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
package org.citsa.ofbiz.beans.service

import org.apache.commons.lang.builder.CompareToBuilder 
import org.citsa.ofbiz.beans.ComponentElement 

class Service implements Comparable<Service> {

	ComponentElement parent
	
	String description
	String nameSpace
	String implementService=""
	boolean optionalImplementService=false
	
	String name
	String engine
	String location=""
	String invoke
	String defaultEntityName
	String transactionTimeOut="0"
	String maxRetry="-1"
	String semaphoreWaitSeconds="300"
	String semaphoreSleep= "500"
	String semaphore="none"
	String auth="false"
	String export="false"
	String validate="true"
	String useTransaction="true"
	String requireNewTransaction="false"
		
	PermissionService permissionService
	
	List<RequiredPermission> requiredPermissions = []
	List<AutoAttribute> autoAttributes = []
	List<Attribute> attributes = []
	List<Override> overrides=[]
	                          
    def populateAttributes(def xml){
		name = xml.'@name'?:""
		engine = xml.'@engine'?:""
		location = xml.'@location'?:""
		invoke = xml.'@invoke'?:""
		defaultEntityName=xml.'@default-entity-name'?:""
		transactionTimeOut=xml.'@transaction-time-out'?:"0"
		maxRetry = xml.'@max-retry'?:"-1"
		semaphoreWaitSeconds=xml.'@semaphore-wait-seconds'?:"300"
		semaphoreSleep=xml.'@semaphore-sleep'?:"500"
		semaphore=xml.'@semaphore'="none"
		auth=xml.'@auth'="false"
		export=xml.'@export'="false"
		validate=xml.'@validate'="true"
		useTransaction=xml.'@use-transaction'="true"
		requireNewTransaction=xml."@require-new-transaction"="false"
		description = xml.'description'?.text()?:""
		nameSpace = xml.'namespace'?.text()?:""
		def implement = xml.'implements'[0]
		                              
		if(implement){			
			implementService = implement.'@service'?:""
			optionalImplementService = implement.'@optional'?:false
		}
		def perms = xml.'permission-service'[0]
		if(perms){
			permissionService=new PermissionService()
			permissionService.populateAttributes(perms)
		}
		def requiredPerms = xml.'required-permissions'
		if(requiredPerms){
			requiredPerms.each{
				req ->
				def perm = new RequiredPermission()
				perm.populateAttributes(req)
			}
		}
		def autoAttrs = xml.'auto-attributes'
		autoAttrs.each{
			attr ->
			AutoAttribute auto = new AutoAttribute()
			auto.populateAttributes(attr)
			autoAttributes << auto
		}
		def attrs  = xml.'attribute'
		attrs.each{
			attr ->
			Attribute att = new Attribute()
			att.populateAttributes(attr)
			attributes << att
		}
		def ovrs = xml.'override'
		ovrs.each{
			ovr ->
			Override override = new Override()
			override.populateAttributes(ovr)
			overrides << override
		}
	}

	public int compareTo(Service o) {
		new CompareToBuilder().append(this.name, o.name)
		.append(this.nameSpace,o.nameSpace)
		.append(this.location,o.location).toComparison()
	}	
	
}
